package Components;



import mainWindow.main;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import DataDefinition.Users;
import java.util.Date;
public class user extends Composite {
	private Text textUser;


	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public user(Composite parent, int style) {
		super(parent, style);
		
		textUser = new Text(this, SWT.BORDER);
		textUser.setBounds(10, 10, 114, 19);
		
		Button btnAdd = new Button(this, SWT.NONE);
		btnAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Users u =parseUser(textUser.getText());
				textUser.setText("");
				if (!main.getUzivatelov().contains(u))
					main.getUzivatelov().add(u);
				else{
					MessageBox msg =  new MessageBox(getShell(),SWT.ICON_ERROR | SWT.ABORT);
					msg.setMessage("Error");
					msg.setText("User already exits");
				}
			}
		});
		btnAdd.setBounds(157, 8, 68, 23);
		btnAdd.setText("Add User");

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	private Users parseUser(String text){
		if(text.length()<=0){
			MessageBox message = new MessageBox(getShell(), SWT.ICON_ERROR| SWT.CANCEL);
			message.setText("Error");
			message.setMessage("Blank field");
			message.open();
		}else if(!text.contains(" ")){
			MessageBox message = new MessageBox(getShell(),SWT.ICON_ERROR | SWT.CANCEL);
			message.setText("Error");
			message.setMessage("You need to eter booth name and surname");
			message.open();
		}
		
		String tmpName = text.substring(0, text.indexOf(" "));
		String tmpSurname = text.substring(text.indexOf(" "));
		if (tmpName.length()<=3){
			MessageBox message = new MessageBox(getShell(), SWT.ICON_ERROR| SWT.CANCEL);
			message.setText("Error");
			message.setMessage("Name too short at least 3 characters");
			message.open();
		}
		if (tmpSurname.length()<=3){
			MessageBox message = new MessageBox(getShell(), SWT.ICON_ERROR| SWT.CANCEL);
			message.setText("Error");
			message.setMessage("Surname too short at least 3 characters");
			message.open();
		}
		if (!Character.isUpperCase(tmpName.charAt(0)))
			Character.toUpperCase(tmpName.charAt(0));
		if (!Character.isUpperCase(tmpSurname.charAt(0)))
			Character.toUpperCase(tmpSurname.charAt(0));
		return new Users(tmpName,tmpSurname,new Date());
	}
}
